package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExcelKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.BookingsInfoAddDTO;
import org.jsola.hr.dto.BookingsInfoUpdateDTO;
import org.jsola.hr.query.BookingsInfoQuery;
import org.jsola.hr.service.IBookingsInfoService;
import org.jsola.hr.vo.BookingsInfoListVO;
import org.jsola.hr.vo.BookingsInfoVO;
import org.jsola.hr.vo.excel.BookingsInfoExportVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * 预约业务人员信息表
 *
 * @author zhr
 */
@RestController("hrBookingsInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/bookingsInfos")
public class BookingsInfoController implements IBookingsInfoControllerAPI {

    @Autowired
    private IBookingsInfoService bookingsInfoService;

    @Override
    @PostMapping
    public Result<BookingsInfoVO> save(@Valid @RequestBody BookingsInfoAddDTO bookingsInfoAddDTO) {
        TokenUser tokenUser = new TokenUser();
        // todo 新增预约业务人员信息表权限校验
        tokenUser.setSiteId("default");
        tokenUser.setUserId("system");
        return Result.success(
                bookingsInfoService.save(bookingsInfoAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/sendBookingsMessage/{phone}")
    public Result<String> sendBookingsMessage(@PathVariable String phone) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增预约业务人员信息表权限校验
        bookingsInfoService.sendBookingsMessage(phone);
        return Result.success();
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody BookingsInfoUpdateDTO bookingsInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改预约业务人员信息表权限校验

        int count = bookingsInfoService.update(bookingsInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{bookingsInfoId}")
    public Result<Integer> delete(@PathVariable Long bookingsInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除预约业务人员信息表权限校验

        int count = bookingsInfoService.deleteByIds(tokenUser, bookingsInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] bookingsInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除预约业务人员信息表权限校验

        int count = bookingsInfoService.deleteByIds(tokenUser, bookingsInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{bookingsInfoId}")
    public Result<BookingsInfoVO> findById(@PathVariable Long bookingsInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看预约业务人员信息表权限校验

        return Result.success(
            bookingsInfoService.selectById(bookingsInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectByUserId")
    public Result<BookingsInfoVO> selectByUserId() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看预约业务人员信息表权限校验

        return Result.success(
                bookingsInfoService.selectByUserId(tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<BookingsInfoListVO>> listAll(@Valid BookingsInfoQuery bookingsInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看预约业务人员信息表权限校验

        return Result.success(
            bookingsInfoService.select(bookingsInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<BookingsInfoListVO>> page(@Valid BookingsInfoQuery bookingsInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            bookingsInfoService.selectPage(bookingsInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/export")
    public ResponseEntity<Resource> export(@Valid BookingsInfoQuery bookingsInfoQuery) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("default");
        tokenUser.setUserId("system");
        // todo 导出权限校验

        // 要导出的数据
        List<BookingsInfoExportVO> bookingsInfoExportVOList = bookingsInfoService.selectExportList(bookingsInfoQuery,tokenUser.getSiteId());
        // 导出excel
        String excelFilePath = ExcelKit.exportExcel("bookingsInfo", "预约业务人员信息表", BookingsInfoExportVO.class, bookingsInfoExportVOList);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE,"application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }


}

